/**
 *@description 用户画像》自然人属性》基本信息
 *@author yhw
 */
import {
  sexQuery,
  ageQuery,
  areaQuery,
  cityQuery,
  xingzuoQuery
} from '../services/PersonsInfo';
import {
  parse
} from 'qs';
import error from '../utils/tips';

export default {

  namespace: 'PersonsInfo',

  state: {
    sexData: null,
    sexLoading: false,
    ageData: null,
    ageLoading: false,
    areaData: [],
    cityData: null,
    xingzuoData: null,
  },

  subscriptions: {},

  effects: { * sexQuery({
      payload
    }, {
      call,
      put
    }) {

      yield put({
        type: 'sexLoading'
      });

      const {
        data
      } = yield call(sexQuery, parse(payload));

      if (data) {

        let temp = {
            legend: [],
            value: []
          },
          colors = ['#ceb180', '#58616b'];
        data.data && data.data.map(function(item, index) {
          //temp.legend.push(item.name);
          temp.value.push({
            name: item.name,
            value: item.value,
            itemStyle: {
              normal: {
                color: colors[index]
              }
            }
          });
        });

        yield put({
          type: 'sexQuerySuccess',
          payload: {
            sexData: temp,
          },
        });
      } else {
        error("获取性别失败");
      }
    },
    //===================================
    * ageQuery({
      payload
    }, {
      call,
      put
    }) {

      yield put({
        type: 'ageLoading'
      });

      const {
        data
      } = yield call(ageQuery, parse(payload));

      if (data) {

        let temp = {
            category: [],
            value: []
          },
          colors = ['#ceb180', '#fff2cc', '#fbe5d6', '#f8cbad', '#ededed', '#deebf7', '#bdd7ee', '#adb9ca', '#f2f2f2', '#d9d9d9', '#ffc86b', '#ffb22f'];
        data.data && data.data.map(function(item, index) {
          temp.category.push(item.name);
          temp.value.push({
            name: item.name,
            value: item.value,
            itemStyle: {
              normal: {
                color: colors[index]
              }
            }
          });
        });

        yield put({
          type: 'ageQuerySuccess',
          payload: {
            ageData: temp,
          },
        });
      } else {
        error("获取年龄失败");
      }
    },
    //===================================
    * areaQuery({
      payload
    }, {
      call,
      put
    }) {

      yield put({
        type: 'areaLoading'
      });

      const {
        data
      } = yield call(areaQuery, parse(payload));

      if (data) {
        yield put({
          type: 'areaQuerySuccess',
          payload: {
            areaData: data.data || [],
          },
        });
      } else {
        error("获取地域特征失败");
      }
    },
    //===================================
    * cityQuery({
      payload
    }, {
      call,
      put
    }) {

      yield put({
        type: 'cityLoading'
      });

      const {
        data
      } = yield call(cityQuery, parse(payload));

      if (data) {

        let temp = {
            category: [],
            value: []
          },
          colors = ['#ceb180', '#fff2cc', '#fbe5d6', '#f8cbad', '#ededed', '#deebf7', '#bdd7ee', '#adb9ca', '#f2f2f2', '#d9d9d9', '#ffc86b', '#ffb22f'];
        data.data && data.data.map(function(item, index) {
          temp.category.push(item.name);
          temp.value.push({
            name: item.name,
            value: item.value,
            itemStyle: {
              normal: {
                color: colors[index]
              }
            }
          });
        });

        yield put({
          type: 'cityQuerySuccess',
          payload: {
            cityData: temp,
          },
        });
      } else {
        error("获取城市失败");
      }
    },
    //===================================
    * xingzuoQuery({
      payload
    }, {
      call,
      put
    }) {

      yield put({
        type: 'xingzuoLoading'
      });

      const {
        data
      } = yield call(xingzuoQuery, parse(payload));

      if (data) {

        let temp = {
            category: [],
            value: []
          },
          colors = ['#d9d9d9', '#f2f2f2', '#adb9ca', '#bdd7ee', '#deebf7', '#ededed', '#f8cbad', '#fbe5d6', '#fff2cc', '#ceb180', '#ffc86b', '#ffb22f'];
        data.data && data.data.map(function(item, index) {
          temp.category.push(item.name);
          temp.value.push({
            name: item.name,
            value: item.value,
            itemStyle: {
              normal: {
                color: colors[index]
              }
            }
          });
        });

        yield put({
          type: 'xingzuoQuerySuccess',
          payload: {
            xingzuoData: temp,
          },
        });
      } else {
        error("获取星座失败");
      }
    },
  },

  reducers: {
    sexLoading(state) {
      return {...state,
        sexLoading: true
      };
    },
    sexQuerySuccess(state, action) {
      return {...state,
        ...action.payload,
        sexLoading: false
      };
    },
    //===================================
    ageLoading(state) {
      return {...state,
        ageLoading: true
      };
    },
    ageQuerySuccess(state, action) {
      return {...state,
        ...action.payload,
        ageLoading: false
      };
    },
    //===================================
    areaLoading(state) {
      return {...state,
        areaLoading: true
      };
    },
    areaQuerySuccess(state, action) {
      return {...state,
        ...action.payload,
        areaLoading: false
      };
    },
    //===================================
    cityLoading(state) {
      return {...state,
        cityLoading: true
      };
    },
    cityQuerySuccess(state, action) {
      return {...state,
        ...action.payload,
        cityLoading: false
      };
    },
    //===================================
    xingzuoLoading(state) {
      return {...state,
        xingzuoLoading: true
      };
    },
    xingzuoQuerySuccess(state, action) {
      return {...state,
        ...action.payload,
        xingzuoLoading: false
      };
    },
  },

};